package com.xiaoyang.lotterysystem.controller.handler;

import com.xiaoyang.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.xiaoyang.lotterysystem.common.exception.ControllerException;
import com.xiaoyang.lotterysystem.common.exception.ServiceException;
import com.xiaoyang.lotterysystem.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice // 捕获全局异常
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceException(ServiceException e) {
        // 打印错误日志
        log.error("ServiceException," + e);

        // 构造错误结果
        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),
                e.getMessage());
    }

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> controllerException(ControllerException e) {
        // 打印错误日志
        log.error("ControllerException," + e);

        // 构造错误结果
        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),
                e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exception(Exception e) {
        // 打印错误日志
        log.error("服务异常," + e);

        // 构造错误结果
        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),
                e.getMessage());
    }
}
